/*
 * Copyright 2008 Federal Emergency Management Agency
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *    
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 *  
 */
package gov.fema.hazus.bdc.client.general.location;

import gov.fema.hazus.bdc.client.controller.Controller;
import gov.fema.hazus.bdc.client.general.GeneralConstants;
import gov.fema.hazus.bdc.client.ui.BuildingEditor;

import com.google.gwt.core.client.GWT;
import com.google.gwt.maps.client.MapType;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.MapTypeControl;
import com.google.gwt.maps.client.control.SmallMapControl;
import com.google.gwt.maps.client.event.MapClickHandler;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.maps.client.overlay.Overlay;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class LocationFinder implements ClickListener {

    private static class MapDialog extends DialogBox 
    {
        
        private GeneralConstants constants;
        
        private MapWidget map;
        private Marker marker;

        private Label latLabel;

        private TextBox lat;

        private Label lngLabel;

        private TextBox lng;
        
        public MapDialog()
        {
            
            super(false);
            
            constants = (GeneralConstants)GWT.create(GeneralConstants.class);
            
            /*
             * Lat and Long labels, with their respective TextBoxes
             */
            latLabel = new Label(constants.generalLocationLatitude());
            lat = new TextBox();
            
            lngLabel = new Label(constants.generalLocationLongitude());
            lng = new TextBox();            
            
            //setSize("300", "250");
            setWidth("440");
            
            setText( constants.generalLocationLocateBuilding() );
            
            VerticalPanel v = new VerticalPanel();
            
            /*
             * Include a map
             */
            map = new MapWidget();
            map.setSize("400px", "300px");
            map.addControl(new SmallMapControl());
            map.addControl(new MapTypeControl());
            map.setCurrentMapType(MapType.getSatelliteMap());
            
            // get latitude and longitude of current building
            // set lat and lng of current building to center of map
            double latitude = Controller.get().buildingEditorGetBuildingLatitude();
            double longitude = Controller.get().buildingEditorGetBuildingLongitude();
            LatLng center = LatLng.newInstance(latitude, longitude);
        	map.setCenter(center);
        	map.setZoomLevel(18);
            
            // update labels
            lat.setText( Double.toString(center.getLatitude()));
            lng.setText( Double.toString(center.getLongitude()));
            
            /*
            if( locationForm != null )
            {
                GeoLocation loc = locationForm.getLocation();
                if( loc != null )
                {
                    
                    LatLng center = new LatLng(loc.getLatitude(), loc.getLongitude());
                    GWT.log("location form loc:" + loc, null);
                    GWT.log("center:" + center, null);
                    map.setCenter(center);
                    map.setZoomLevel(18);
                    
                    lat.setText( Double.toString(center.getLatitude()));
                    lng.setText( Double.toString(center.getLongitude()));
                    
                }
            }
            */
            
            map.addMapClickHandler(new MapClickHandler(){
    
                public void onClick(MapClickEvent event) {
                    LatLng point = event.getLatLng();
                    //marker.setPoint(point);
                    marker.setLatLng(point);
                    String lt = Double.toString(point.getLatitude());
                    String ln = Double.toString(point.getLongitude());
                    lat.setText( lt );
                    lng.setText( ln );
                }
                
            });
            marker = new Marker(map.getCenter());
            marker.isDraggable();
            map.addOverlay(marker);
            
            /*
             * An OK button
             */
            Button ok = new Button(constants.generalLocationOK());
            ok.addClickListener(new ClickListener(){
    
                public void onClick(Widget sender) {
                    LatLng p = marker.getLatLng();
                    //TODO: insert lat/lng into form via JSNI
                    GWT.log("Sending clicked lat/lng to controller...", null);
                    //locationForm.setPointAsLocation(p);
                    // Controller.get().buildingEditorUpdateLatLng(p.getLatitude(), p.getLongitude());
                    BuildingEditor.get().updateLatLng(p.getLatitude(), p.getLongitude());
                    MapDialog.this.hide();
                }
                
            });
            Button cancel = new Button(constants.generalLocationCancel());
            cancel.addClickListener(new ClickListener(){
    
                public void onClick(Widget sender) {
                    MapDialog.this.hide();
                }
                
            });
            HorizontalPanel buttons = new HorizontalPanel();
            buttons.add(ok);
            buttons.add(cancel);
                        
            
            


            FlexTable t = new FlexTable();
            t.setWidget(0,0,latLabel);
            t.setWidget(0,1,lat);
            t.setWidget(1,0,lngLabel);
            t.setWidget(1,1,lng);
            
            
            v.add(map);
            v.add(t);
            v.add(buttons);
            
            v.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);

            setWidget(v);
        
        }
    }
    
    private MapWidget map;
    
    public LocationFinder()
    {

    }

    public void onClick(Widget sender) {
        MapDialog md = new MapDialog();
        md.center();
        md.show();
    }

}